package org.example.hzit.controller;


import org.example.hzit.OrderVO.OrderVo;
import org.example.hzit.client.userClient;
import org.example.hzit.entity.Order;
import org.example.hzit.entity.User;
import org.example.hzit.service.IOrderService;
import org.example.hzit.util.PageResult;
import org.example.hzit.util.R;
import org.example.hzit.util.R1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gww
 * @since 2025-01-16
 */
@RestController
@RequestMapping("/order")
@CrossOrigin(origins = "http://localhost:8082")
public class OrderController {

    @Autowired
    private IOrderService orderService;

    @Autowired
    private userClient client;




    @GetMapping("/list")
    public R getorder(){
        return R.ok(orderService.list());
    }

    @GetMapping("/user/list")
    public R<List<User>>userList(){
        return client.list();
    }

    @GetMapping("/findByPage/{page}/{pageSize}")
    public R findByPage(@PathVariable("page") Integer page, @PathVariable("pageSize") Integer pageSize){
        PageResult<Order> wo = orderService.findByPage(page,pageSize);
        return R.ok(wo);
    }

    @PostMapping("/search/{page}/{pageSize}")
    public R search(@PathVariable("page") Integer page,@PathVariable("pageSize") Integer pageSize,
                    @RequestBody(required = false) OrderVo vo){
        PageResult<Order>wo=orderService.search(page,pageSize,vo);
        return R.ok(wo);
    }

    @PostMapping("/order")
    public Order addOrder(@RequestBody Order order) {
        return orderService.addOrder(order);
    }

    @PutMapping("/update")
    public R1 update(@RequestBody Order order){
        boolean flag=orderService.updateById(order);
        return flag ? R1.ok().message("修改信息成功") :R1.error().message("修改信息失败");
    }


    @DeleteMapping("/delete/{ids}")
    public R1 delete(@PathVariable("ids") String ids){
        String[] split = ids.split(",");
        List<String> sids = Arrays.asList(split);
        return orderService.removeByIds(sids) ? R1.ok().message("删除学生成功!") : R1.error().message("删除学生失败!");
    }




}
